package com.mxpio.erp.workshop.policy.impl;

import com.mxpio.erp.common.LotStatusUtil;
import com.mxpio.erp.common.enums.*;
import com.mxpio.erp.common.inventory.entity.*;
import com.mxpio.erp.common.jpa.model.Notice;
import com.mxpio.erp.common.jpa.model.NoticeLine;
import com.mxpio.erp.common.jpa.model.NoticeLineKey;
import com.mxpio.erp.common.jpa.model.OrderLineKey;
import com.mxpio.erp.common.order.policy.AbstractNoticePolicy;
import com.mxpio.erp.common.order.policy.NoticeContext;
import com.mxpio.erp.common.purchase.entity.RejectNotice;
import com.mxpio.erp.common.purchase.entity.RejectNoticeLine;
import com.mxpio.erp.common.purchase.entity.Supply;
import com.mxpio.erp.common.technology.entity.Item;
import com.mxpio.erp.common.technology.entity.ItemPlanProp;
import com.mxpio.erp.common.vo.OrderExcuteLineVo;
import com.mxpio.erp.common.workshop.entity.DefectiveProductsNoticeLine;
import com.mxpio.erp.common.workshop.entity.ManufactureOrder;
import com.mxpio.erp.common.workshop.entity.ManufactureOrderItem;
import com.mxpio.erp.common.workshop.entity.MoPickingNotice;
import com.mxpioframework.common.exception.MBootException;
import com.mxpioframework.common.util.BeanReflectionUtils;
import com.mxpioframework.jpa.JpaUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 不良品退料通知单执行策略
 * @author mxpio
 *
 */
public class DefaultDefectiveNoticePolicy extends AbstractNoticePolicy {

	private List<OrderExcuteLineVo> orderExcuteVos;

	public DefaultDefectiveNoticePolicy(List<OrderExcuteLineVo> orderExcuteVos) {
		this.orderExcuteVos = orderExcuteVos;
	}
	
	@Override
	public void execute(NoticeContext context) {
		
	}
	
	@Override
	public NoticeContext config(NoticeContext context) {
		context.setOrderExcuteVos(orderExcuteVos);
		return context;
	}

	//重写apply方法，根据执行提交的明细数据，是否补料。直接生成一个对应的补料通知单。同时更新原通知单明细中的异常信息登记字段
	@Override
	public void apply(NoticeContext context) {
		if(beforeExecute(context)) {
			execute(context);
			Notice notice = context.getEntity();
			List<OrderExcuteLineVo> orderExcuteVos = context.getOrderExcuteVos();
			//区分jit物料和普通物料
			//jit物料
			List<OrderExcuteLineVo> jitVos = new ArrayList<>();
			//非jit物料
			List<OrderExcuteLineVo> unJitVos = new ArrayList<>();

			Set<String> itemCodeSet = orderExcuteVos.stream().map(OrderExcuteLineVo::getItemCode).collect(Collectors.toSet());
			List<Item> itemList = JpaUtil.linq(Item.class).in("itemCode", itemCodeSet).list();
			Map<Object, Item> itemMap = JpaUtil.index(itemList, "itemCode");

			List<ItemPlanProp> itemPlanPropList = JpaUtil.linq(ItemPlanProp.class).equal("propType", "item").in("code", itemCodeSet).list();
			Map<String, ItemPlanProp> itemPlanPropMap = JpaUtil.index(itemPlanPropList, "code");
			for(OrderExcuteLineVo vo : orderExcuteVos){
				if (itemPlanPropMap.containsKey(vo.getItemCode())
						&& ItemEnums.FeedingMode.JIT.getTypeCode().equals(itemPlanPropMap.get(vo.getItemCode()).getFeedingMode())){
					jitVos.add(vo);
				}else{
					unJitVos.add(vo);
				}
			}

			//处理jit物料
			if (jitVos.size()>0){
				//0，生成退货入库仓单，且自动执行()
				WarehouseOrder inWo = null;
				int lineNo = 0;
				for(OrderExcuteLineVo vo : jitVos){
					//回写不良品退料通知单明细中的字段数据
					NoticeLineKey noticeLineKey = new NoticeLineKey();
					noticeLineKey.setNoticeNo(vo.getNoticeNo());
					noticeLineKey.setLineNo(vo.getNoticeLineNo());
					DefectiveProductsNoticeLine dBDefectiveProductsNoticeLine = JpaUtil.getOne(DefectiveProductsNoticeLine.class, noticeLineKey);
					if (dBDefectiveProductsNoticeLine!=null){
						dBDefectiveProductsNoticeLine.setReporter(vo.getReporter());
						dBDefectiveProductsNoticeLine.setReportProc(vo.getReportProc());
						dBDefectiveProductsNoticeLine.setExceptionType(vo.getExceptionType());
						dBDefectiveProductsNoticeLine.setDefectiveReason(vo.getDefectiveReason());
						dBDefectiveProductsNoticeLine.setDefectiveDesc(vo.getDefectiveDesc());
						dBDefectiveProductsNoticeLine.setResponseUnit(vo.getResponseUnit());
						JpaUtil.update(dBDefectiveProductsNoticeLine);
					}

					//生产仓单
					lineNo++;
					if(inWo == null){
						inWo = new WarehouseOrder();
						inWo.setAccessType(notice.getAccessType());
						inWo.setBpmnStatus(OrderStatusEnums.BpmnStatus.AUDITED.getStatusCode());
						inWo.setCloseStatus(OrderStatusEnums.CloseStatus.OPEN.getStatusCode());
						inWo.setOrderStatus(OrderStatusEnums.OrderStatus.FINISHED.getStatusCode());
						inWo.setOrderDate(new Date());
						inWo.setOriginBizNo(vo.getBizOrderNo());
						inWo.setWhCode(vo.getWhCode());//前端已经传递报废属性的仓库
						//wo.setWhCode(warehouseList.get(0).getWhCode());//生成的仓单对应的仓库应该是仓储属性是报废的仓库
						inWo.setWoType(notice.getPickingType());
						inWo.setOriginNoticeNo(notice.getNoticeNo());//关联noticeno字段
						inWo.setExecuteTime(new Date());
						inWo.setExecutor("8888");//自动化助手，默认指定用户
						JpaUtil.save(inWo);
					}
					WarehouseOrderLine inWoLine = new WarehouseOrderLine();
					inWoLine.setWhCode(inWo.getWhCode());
					inWoLine.setCloseStatus(OrderStatusEnums.CloseStatus.OPEN.getStatusCode());
					Item item = itemMap.get(vo.getItemCode());
					inWoLine.setItemCode(vo.getItemCode());
					inWoLine.setItemGroupCode(item.getItemGroupCode());
					inWoLine.setItemName(item.getItemName());
					inWoLine.setItemSpec(item.getItemSpec());
					inWoLine.setUnitCode(item.getUnitCode());
					inWoLine.setDrawingNo(item.getDrawingNo());
					inWoLine.setQuantity(vo.getQuantity());
					inWoLine.setBizNo(inWo.getBizNo());
					inWoLine.setLineNo(lineNo+"");
					inWoLine.setOriginBizNo(vo.getBizOrderNo());
					inWoLine.setOriginBizLineNo(vo.getBizOrderlineNo());
					inWoLine.setSort(vo.getSort());
					inWoLine.setLotNo(vo.getLotNo());//
					//拆批 新生成批次索引
					Long count = JpaUtil.linq(Lot.class).equal("lotNo", vo.getLotNo()).count();
					inWoLine.setSubLotIndex(count.toString());//子批索引
					Lot lot = new Lot();
					lot.setLotNo(vo.getLotNo());
					String binaryString = LotStatusUtil.toBinaryString("0");//默认正常
					lot.setLotStatus(LotStatusUtil.switchBinaryByIndex(binaryString, InspectionEnums.LotStatusBitIndex.IB14.getIndex()));

					lot.setSubLotIndex(inWoLine.getSubLotIndex());
					JpaUtil.save(lot);

					vo.setSplitLot(lot);//后边退货单使用

					inWoLine.setOriginNoticeNo(notice.getNoticeNo());
					inWoLine.setOriginNoticeLineNo(vo.getNoticeLineNo());
					JpaUtil.save(inWoLine);

					//自动执行，生成流水
					InvFlow inFlow = new InvFlow();
					inFlow.setAccessType(inWo.getAccessType());
					inFlow.setItemCode(inWoLine.getItemCode());
					inFlow.setWhCode(inWo.getWhCode());
					inFlow.setQuantity(inWoLine.getQuantity());
					inFlow.setMemo(inWo.getMemo());
					inFlow.setOriginBizNo(inWo.getOriginBizNo());
					inFlow.setOriginBizType(inWo.getOriginBizType());
					inFlow.setOriginBizLineNo(inWoLine.getOriginBizLineNo());
					inFlow.setWoBizNo(inWo.getBizNo());
					inFlow.setWoBizType(inWo.getWoType());
					inFlow.setWoBizLineNo(inWoLine.getLineNo());
					inFlow.setLotNo(inWoLine.getLotNo());
					inFlow.setSubLotIndex(inWoLine.getSubLotIndex());//增加子批索引
					JpaUtil.save(inFlow);
				}

				//1，生成采购退货通知单及仓单，并自动执行
				for(OrderExcuteLineVo vo : jitVos){
					String lotNo = vo.getLotNo();
					String subLotIndex = vo.getSubLotIndex();
					String originBizNo = "";
					String originBizLineNo = "";
					List<InvFlow> invFlows = JpaUtil.linq(InvFlow.class)
							.equal("lotNo", lotNo)
							.equal("subLotIndex", subLotIndex)
							.equal("woBizType", "10")
							.equal("accessType", "in").list();//考虑常规物料采购入库，后续改为jit物料的情况(追溯源采购订单)
					if (invFlows.size()>0){
						originBizNo = invFlows.get(0).getOriginBizNo();
						originBizLineNo = invFlows.get(0).getOriginBizLineNo();
					}
					List<InvFlow> invFlows2 = JpaUtil.linq(InvFlow.class)
							.equal("lotNo", lotNo)
							.equal("subLotIndex", subLotIndex)
							.equal("woBizType", "57")
							.equal("accessType", "in").list();//查找送货计划产生的入库流水
					if(invFlows2.size()>0){
						originBizNo = invFlows2.get(0).getOriginBizNo();
						originBizLineNo = invFlows2.get(0).getOriginBizLineNo();
					}

					//自动生成退货通知单，
					RejectNotice rejectNotice = new RejectNotice();
					rejectNotice.setBizOrderNo(originBizNo);
					rejectNotice.setNoticeDate(new Date());
					rejectNotice.setWhCode(vo.getWhCode());
					rejectNotice.setAccessType(OrderEnums.AccessType.OUT.getCode());
					rejectNotice.setPickingType(NoticeStatusEnums.NoticeType.PO_OUT.getCode());
					/*LotKey lotKey = new LotKey();
					lotKey.setLotNo(lotNo);
					lotKey.setSubLotIndex(subLotIndex);*/
					Lot lot = vo.getSplitLot();//取新拆批批次
					String rejectType = "";//值对应通知单退货类型字典值
					if (StringUtils.equals(lot.getLotStatus(), LotStatusUtil.toBinaryString("0"))){
						rejectType = "0";//合格品退货
					}else{
						rejectType = "1";//不合格品退货
					}
					rejectNotice.setRejectType(rejectType);
					rejectNotice.setNoticeStatus(NoticeStatusEnums.NoticeStatus.CONFIRM.getStatusCode());

					JpaUtil.save(rejectNotice);

					NoticeLine rejectNoticeLine = new RejectNoticeLine();
					rejectNoticeLine.setOriginBizLineNo(originBizLineNo);
					rejectNoticeLine.setOriginBizNo(originBizNo);
					rejectNoticeLine.setItemCode(vo.getItemCode());
					rejectNoticeLine.setNoticeNo(rejectNotice.getNoticeNo());
					rejectNoticeLine.setQuantity(vo.getQuantity());
					rejectNoticeLine.setLotNo(lot.getLotNo());
					rejectNoticeLine.setSubLotIndex(lot.getSubLotIndex());
					rejectNoticeLine.setLineNo("1");
					Item item = JpaUtil.linq(Item.class).idEqual(vo.getItemCode()).findOne();
					rejectNoticeLine.setItemName(item.getItemName());
					rejectNoticeLine.setItemSpec(item.getItemSpec());
					rejectNoticeLine.setUnitCode(item.getUnitCode());
					rejectNoticeLine.setDrawingNo(item.getDrawingNo());
					JpaUtil.save(rejectNoticeLine);

					//通知单默认确认，自动生成仓单
					WarehouseOrder outWo = new WarehouseOrder();
					outWo.setAccessType(rejectNotice.getAccessType());
					outWo.setBpmnStatus(OrderStatusEnums.BpmnStatus.AUDITED.getStatusCode());
					outWo.setCloseStatus(OrderStatusEnums.CloseStatus.OPEN.getStatusCode());
					outWo.setOrderStatus(OrderStatusEnums.OrderStatus.FINISHED.getStatusCode());
					outWo.setOrderDate(new Date());
					outWo.setOriginBizNo(rejectNotice.getBizOrderNo());
					outWo.setWhCode(rejectNotice.getWhCode());
					outWo.setWoType(rejectNotice.getPickingType());
					outWo.setOriginNoticeNo(rejectNotice.getNoticeNo());//关联noticeno字段
					outWo.setExecuteTime(new Date());
					outWo.setExecutor("8888");//自动化助手，默认指定用户
					JpaUtil.save(outWo);
					//仓单明细
					WarehouseOrderLine outWoLine = new WarehouseOrderLine();
					outWoLine.setWhCode(outWo.getWhCode());
					outWoLine.setCloseStatus(OrderStatusEnums.CloseStatus.OPEN.getStatusCode());
					outWoLine.setItemCode(rejectNoticeLine.getItemCode());
					outWoLine.setItemGroupCode(item.getItemGroupCode());
					outWoLine.setItemName(item.getItemName());
					outWoLine.setItemSpec(item.getItemSpec());
					outWoLine.setUnitCode(item.getUnitCode());
					outWoLine.setDrawingNo(item.getDrawingNo());
					outWoLine.setQuantity(rejectNoticeLine.getQuantity());
					outWoLine.setBizNo(outWo.getBizNo());
					outWoLine.setLineNo("1");
					outWoLine.setOriginBizNo(rejectNoticeLine.getOriginBizNo());
					outWoLine.setOriginBizLineNo(rejectNoticeLine.getOriginBizLineNo());
					outWoLine.setLotNo(lot.getLotNo());
					outWoLine.setSubLotIndex(lot.getSubLotIndex());
					outWoLine.setOriginNoticeNo(rejectNoticeLine.getNoticeNo());
					outWoLine.setOriginNoticeLineNo(rejectNoticeLine.getLineNo());
					JpaUtil.save(outWoLine);

					//自动执行，生成出流水
					InvFlow outFlow = new InvFlow();
					outFlow.setAccessType(outWo.getAccessType());
					outFlow.setItemCode(outWoLine.getItemCode());
					outFlow.setWhCode(outWo.getWhCode());
					outFlow.setQuantity(outWoLine.getQuantity());
					outFlow.setMemo(outWo.getMemo());
					outFlow.setOriginBizNo(outWo.getOriginBizNo());
					outFlow.setOriginBizType(outWo.getOriginBizType());
					outFlow.setOriginBizLineNo(outWoLine.getOriginBizLineNo());
					outFlow.setWoBizNo(outWo.getBizNo());
					outFlow.setWoBizType(outWo.getWoType());
					outFlow.setWoBizLineNo(outWoLine.getLineNo());
					outFlow.setLotNo(outWoLine.getLotNo());
					outFlow.setSubLotIndex(outWoLine.getSubLotIndex());//增加子批索引
					JpaUtil.save(outFlow);
				}
				//3.补料处理逻辑：
				Map<String, ManufactureOrder> orderMap = new HashMap<>();
				for(OrderExcuteLineVo vo : jitVos){
					if ("1".equals(vo.getSupplementFlag())
							||"Y".equals(vo.getSupplementFlag())
							||"是".equals(vo.getSupplementFlag())){//是否需要生成补料通知单

						MoPickingNotice noticeAdd = (MoPickingNotice) BeanReflectionUtils.newInstance(MoPickingNotice.class);

						ManufactureOrder order = null;//源生产订单
						if (orderMap.containsKey(vo.getBizOrderNo())){
							order = orderMap.get(vo.getBizOrderNo());
						}else{
							order = JpaUtil.getOne(ManufactureOrder.class, vo.getBizOrderNo());
							if (order!=null){
								orderMap.put(vo.getBizOrderNo(),order);
							}
						}
						//关联生产订单业务字段
						if (order!=null){
							noticeAdd.setProductItemCode(order.getProductItemCode());
							noticeAdd.setProductItemName(order.getProductItemName());
							noticeAdd.setOrderQuantity(order.getOrderQuantity());
							noticeAdd.setProductDrawingNo(order.getProductDrawingNo());
							noticeAdd.setMainWorkshop(order.getMainWorkshop());
						}
						noticeAdd.setBizOrderNo(vo.getBizOrderNo());
						noticeAdd.setNoticeDate(new Date());
						noticeAdd.setWhCode(vo.getBomWhCode());//取原订单bom子表中的仓库信息
						noticeAdd.setAccessType(OrderEnums.AccessType.OUT.getCode());
						noticeAdd.setPickingType(NoticeStatusEnums.NoticeType.MO_M_REOUT.getCode());//生产补料
						noticeAdd.setNoticeStatus(NoticeStatusEnums.NoticeStatus.CREATE.getStatusCode());

						JpaUtil.save(noticeAdd);

						NoticeLine line = noticeAdd.createLine(vo);
						line.setLineNo("1");
						line.setLotNo("");//去掉批次信息
						line.setSubLotIndex("");//去掉批次信息
						if (itemMap.containsKey(vo.getItemCode())) {
							Item item = itemMap.get(vo.getItemCode());
							line.setItemName(item.getItemName());
							line.setItemSpec(item.getItemSpec());
							line.setUnitCode(item.getUnitCode());
							line.setDrawingNo(item.getDrawingNo());
						}
						JpaUtil.save(line);

						//生成送货计划
						SupplyChain one = JpaUtil.linq(SupplyChain.class)
								.collect("pnCode", Supply.class, "pnCode")
								.equal("itemCode", vo.getItemCode())
								.equal("defaultSupply", CommonEnums.YesNo.YES.getCode()).findOne();

						FeedPlanNotice feedPlanNotice = new FeedPlanNotice();
						feedPlanNotice.setSourcePickingNoticeNo(line.getNoticeNo());
						feedPlanNotice.setWhCode(noticeAdd.getWhCode());//取补料通知单对应仓库
						feedPlanNotice.setNoticeStatus(NoticeStatusEnums.NoticeStatus.DRAFT.getStatusCode());
						feedPlanNotice.setNoticeDate(new Date());
						feedPlanNotice.setAccessType("in");
						feedPlanNotice.setPickingType(NoticeStatusEnums.NoticeType.FP_IN.getCode());
						feedPlanNotice.setBizOrderNo(order.getBizNo());
						//生产订单主表字段
						feedPlanNotice.setProductItemCode(order.getProductItemCode());
						feedPlanNotice.setProductItemName(order.getProductItemName());
						feedPlanNotice.setProductDrawingNo(order.getProductDrawingNo());
						if (one != null) {
							feedPlanNotice.setPnCode(one.getPnCode());
							feedPlanNotice.setPnName(one.getSupply().getPnName());
						}else{
							throw new MBootException("物料"+line.getItemCode()+"对应默认供应商获取有误，无法正常执行生成送货计划，请检查供应链配置！");
						}
						JpaUtil.save(feedPlanNotice);

						//查询生产订单bom
						OrderLineKey lineKey = new OrderLineKey();
						lineKey.setLineNo(line.getOriginBizLineNo());
						lineKey.setBizNo(order.getBizNo());
						ManufactureOrderItem dbMoLine = JpaUtil.getOne(ManufactureOrderItem.class, lineKey);

						FeedPlanNoticeLine feedPlanNoticeLine = new FeedPlanNoticeLine();
						feedPlanNoticeLine.setNoticeNo(feedPlanNotice.getNoticeNo());
						feedPlanNoticeLine.setSourcePickingNoticeNo(line.getNoticeNo());
						feedPlanNoticeLine.setSourcePickingNoticeLineNo(line.getLineNo());
						feedPlanNoticeLine.setItemCode(line.getItemCode());
						feedPlanNoticeLine.setItemName(line.getItemName());
						feedPlanNoticeLine.setItemSpec(line.getItemSpec());
						feedPlanNoticeLine.setUnitCode(line.getUnitCode());
						feedPlanNoticeLine.setDrawingNo(line.getDrawingNo());
						feedPlanNoticeLine.setQuantity(line.getQuantity());
						feedPlanNoticeLine.setSort("1");
						feedPlanNoticeLine.setLineNo("1");
						feedPlanNoticeLine.setOriginBizNo(order.getBizNo());
						feedPlanNoticeLine.setOriginBizLineNo(line.getOriginBizLineNo());

						//冗余展示字段
						feedPlanNoticeLine.setMaterialBrand(dbMoLine.getMaterialBrand());
						feedPlanNoticeLine.setMaterialType(dbMoLine.getMaterialType());
						feedPlanNoticeLine.setMakeLength(dbMoLine.getMakeLength());
						feedPlanNoticeLine.setMakeArea(dbMoLine.getMakeArea());
						feedPlanNoticeLine.setMakeNum(dbMoLine.getMakeNum());
						JpaUtil.save(feedPlanNoticeLine);

					}
				}
			}
			//非jit物料
			if (unJitVos.size()>0){
				WarehouseOrder wo = null;
				int lineNo = 0;
				//补料通知单
				Map<String, MoPickingNotice> noticeAddMap = new HashMap<>();
				Map<String,List<OrderExcuteLineVo>> voAddMap = new HashMap<>();
				Map<String, ManufactureOrder> orderMap = new HashMap<>();
				for(OrderExcuteLineVo vo : unJitVos){
					//回写不良品退料通知单明细中的字段数据
					NoticeLineKey noticeLineKey = new NoticeLineKey();
					noticeLineKey.setNoticeNo(vo.getNoticeNo());
					noticeLineKey.setLineNo(vo.getNoticeLineNo());
					DefectiveProductsNoticeLine dBDefectiveProductsNoticeLine = JpaUtil.getOne(DefectiveProductsNoticeLine.class, noticeLineKey);
					if (dBDefectiveProductsNoticeLine!=null){
						dBDefectiveProductsNoticeLine.setReporter(vo.getReporter());
						dBDefectiveProductsNoticeLine.setReportProc(vo.getReportProc());
						dBDefectiveProductsNoticeLine.setExceptionType(vo.getExceptionType());
						dBDefectiveProductsNoticeLine.setDefectiveReason(vo.getDefectiveReason());
						dBDefectiveProductsNoticeLine.setDefectiveDesc(vo.getDefectiveDesc());
						dBDefectiveProductsNoticeLine.setResponseUnit(vo.getResponseUnit());
						JpaUtil.update(dBDefectiveProductsNoticeLine);
					}
					if ("1".equals(vo.getSupplementFlag())
							||"Y".equals(vo.getSupplementFlag())
							||"是".equals(vo.getSupplementFlag())){//是否需要生成补料通知单
						MoPickingNotice noticeAdd = noticeAddMap.get(vo.getBomWhCode());
						if(noticeAdd == null) {
							noticeAdd = (MoPickingNotice) BeanReflectionUtils.newInstance(MoPickingNotice.class);
							noticeAdd.setBizOrderNo(vo.getBizOrderNo());
							ManufactureOrder order = null;//源生产订单
							if (orderMap.containsKey(vo.getBizOrderNo())){
								order = orderMap.get(vo.getBizOrderNo());
							}else{
								order = JpaUtil.getOne(ManufactureOrder.class, vo.getBizOrderNo());
								if (order!=null){
									orderMap.put(vo.getBizOrderNo(),order);
								}
							}
							//关联生产订单业务字段
							if (order!=null){
								noticeAdd.setProductItemCode(order.getProductItemCode());
								noticeAdd.setProductItemName(order.getProductItemName());
								noticeAdd.setOrderQuantity(order.getOrderQuantity());
								noticeAdd.setProductDrawingNo(order.getProductDrawingNo());
								noticeAdd.setMainWorkshop(order.getMainWorkshop());
							}

							noticeAdd.setNoticeDate(new Date());
							noticeAdd.setWhCode(vo.getBomWhCode());//取原订单bom子表中的仓库信息
							noticeAdd.setAccessType(OrderEnums.AccessType.OUT.getCode());
							noticeAdd.setPickingType(NoticeStatusEnums.NoticeType.MO_M_REOUT.getCode());//生产补料
							noticeAdd.setNoticeStatus(NoticeStatusEnums.NoticeStatus.CREATE.getStatusCode());
							noticeAddMap.put(vo.getBomWhCode() , noticeAdd);
							voAddMap.put(vo.getBomWhCode() , new ArrayList<OrderExcuteLineVo>());
						}
						voAddMap.get(vo.getBomWhCode()).add(vo);
					}
					//生产仓单
					lineNo++;
					if(wo == null){
						wo = new WarehouseOrder();
						wo.setAccessType(notice.getAccessType());
						wo.setBpmnStatus(OrderStatusEnums.BpmnStatus.AUDITED.getStatusCode());
						wo.setCloseStatus(OrderStatusEnums.CloseStatus.OPEN.getStatusCode());
						wo.setOrderStatus(OrderStatusEnums.OrderStatus.PLAN.getStatusCode());
						wo.setOrderDate(new Date());
						wo.setOriginBizNo(vo.getBizOrderNo());
						wo.setWhCode(vo.getWhCode());//前端已经传递报废属性的仓库
						//wo.setWhCode(warehouseList.get(0).getWhCode());//生成的仓单对应的仓库应该是仓储属性是报废的仓库
						wo.setWoType(notice.getPickingType());
						wo.setOriginNoticeNo(notice.getNoticeNo());//关联noticeno字段
						JpaUtil.save(wo);
					}
					WarehouseOrderLine line = new WarehouseOrderLine();
					line.setWhCode(wo.getWhCode());
					line.setCloseStatus(OrderStatusEnums.CloseStatus.OPEN.getStatusCode());
					Item item = JpaUtil.linq(Item.class).idEqual(vo.getItemCode()).findOne();
					line.setItemCode(vo.getItemCode());
					line.setItemGroupCode(item.getItemGroupCode());
					line.setItemName(item.getItemName());
					line.setItemSpec(item.getItemSpec());
					line.setUnitCode(item.getUnitCode());
					line.setDrawingNo(item.getDrawingNo());
					line.setQuantity(vo.getQuantity());
					line.setBizNo(wo.getBizNo());
					line.setLineNo(lineNo+"");
					line.setOriginBizNo(vo.getBizOrderNo());
					line.setOriginBizLineNo(vo.getBizOrderlineNo());
					line.setSort(vo.getSort());
					line.setLotNo(vo.getLotNo());//
					//拆批 新生成批次索引
					Long count = JpaUtil.linq(Lot.class).equal("lotNo", vo.getLotNo()).count();
					line.setSubLotIndex(count.toString());//子批索引
					//
					Lot lot = new Lot();
					lot.setLotNo(vo.getLotNo());
					String binaryString = LotStatusUtil.toBinaryString("0");//默认正常
					lot.setLotStatus(LotStatusUtil.switchBinaryByIndex(binaryString, InspectionEnums.LotStatusBitIndex.IB14.getIndex()));

					lot.setSubLotIndex(line.getSubLotIndex());
					JpaUtil.save(lot);

					line.setOriginNoticeNo(notice.getNoticeNo());
					line.setOriginNoticeLineNo(vo.getNoticeLineNo());
					JpaUtil.save(line);
				}

				// 生成补料通知单明细
				if (!noticeAddMap.isEmpty()){
					for(Map.Entry<String,MoPickingNotice> entry : noticeAddMap.entrySet()) {
						MoPickingNotice addnotice = entry.getValue();
						JpaUtil.save(addnotice);
						int innerlineNo = 0;
						for(OrderExcuteLineVo vo : voAddMap.get(entry.getKey())) {
							innerlineNo++;
							NoticeLine line = addnotice.createLine(vo);
							line.setLineNo(innerlineNo+"");
							line.setLotNo("");//去掉批次信息
							line.setSubLotIndex("");//去掉批次信息
							Item item = JpaUtil.linq(Item.class).idEqual(vo.getItemCode()).findOne();
							line.setItemName(item.getItemName());
							line.setItemSpec(item.getItemSpec());
							line.setUnitCode(item.getUnitCode());
							line.setDrawingNo(item.getDrawingNo());
							JpaUtil.save(line);
						}
					}
				}
			}
			afterExecute(context);
			notice.setNoticeStatus(NoticeStatusEnums.NoticeStatus.CONFIRM.getStatusCode());
			JpaUtil.update(notice);
		} else {
			if(CollectionUtils.isEmpty(context.getErrors())) {
				context.addError("前置校验不通过");
			}
		}

	}

	@Override
	public boolean beforeExecute(NoticeContext context) {
		return true;
	}

	@Override
	public void afterExecute(NoticeContext context) {
		
	}

}
